﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _7.ConvertFromToDesireNumeralSystem
{
    class ConvertFromToDesireNumeralSystem
    {
        static void Main(string[] args)
        {
            Console.Write("Which base to use: ");
            int fromS = int.Parse(Console.ReadLine());
            
            Console.Write("To which base to convert: ");
            
            int toBaseD = int.Parse(Console.ReadLine());
            Console.Write("Number: ");
            
            string numberToConvert = Console.ReadLine();
            
            int sumOfDigits = 0;
            int index = numberToConvert.Length - 1;
            int digit = 0;
            for (int i = 0; i < numberToConvert.Length; i++)
            {
                string part = numberToConvert.Substring(index, 1);
                digit = Convert.ToInt32(part) * (int)Math.Pow(fromS, i);
                sumOfDigits = sumOfDigits + digit;
                index--;
            }
            Console.WriteLine();
            Console.Write("Result:");

            int counter = 0;
            for (int i = 31; i >= 0; i--)
            {
                int exponent = (int)Math.Pow(toBaseD, i);
                digit = sumOfDigits / exponent;
                sumOfDigits = sumOfDigits % exponent;
                if (digit != 0)
                {
                    counter++;
                }
                if (counter > 0)
                {
                    Console.Write(digit);
                }
            }

        }
    }
}